//
// Copyright 2007 Brian Watkins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

function getEvents ( days )
{
  var surl = 'event?action=lookupEvents&eventDate=currentDate&numDays=' + days;

  var callback = 
  {
    success : parseEventDoc,
    failure : handleFailure
  };

  YAHOO.util.Connect.asyncRequest( 'GET', surl, callback );
}

function parseEventDoc ( obj )
{
  var cDates = obj.responseXML.getElementsByTagName( "calendarDate" );

  for ( var x = 0; x < cDates.length; x++ ) {
    var calDateId = cDates.item( x ).getAttribute( "id" );
    var year = cDates.item( x ).getAttribute( "year" );
    var month = cDates.item( x ).getAttribute( "month" )*1 - 1;
    var day = cDates.item( x ).getAttribute( "date" );
    var diw = cDates.item( x ).getAttribute( "dayInWeek" );

    var events = cDates.item( x ).getElementsByTagName( "event" );
    for ( var i = 0; i < events.length; i++ ) {
      var id = year + "-" + month + "-" + day + "-" + i;
      var startTime = events.item( i ).getAttribute( "start" );
      var endTime = events.item( i ).getAttribute( "end" );
      var type = events.item( i ).getAttribute( "type" );
      var data = events.item( i ).firstChild.nodeValue;

      var time = startTime;
      if ( startTime == '12:00am' && endTime == '12:00am' ) {
        time = '';
      } 

      //this is a little wasteful. probably should filter on the server side
      if ( type == 1 || type == 3 ) {
        var evt = makeEvent( id, formatEventDate( year, month, day, diw, time ), data, time );
	evt.setAttribute( "eventDate", calDateId );
	evt.setAttribute( "eventId", i );

        document.getElementById( "events" ).appendChild( evt );
      }
    }
  }
}

function makeEvent ( id, evtDate, data, evtTime )
{
  var note = document.createElement( "div" );
  note.setAttribute( "class", "event" );
  note.setAttribute( "id", id );

  var tab = document.createElement( "TABLE" );
  tab.setAttribute( "width", "100%" );
  tab.setAttribute( "cellpadding", "0" );
  tab.setAttribute( "cellspacing", "0" );
  tab.setAttribute( "border", "0" );
  var tr = document.createElement( "TR" );

  var title = document.createElement( "TD" );
  title.setAttribute( "class", "eventTitle" );
  title.innerHTML = evtDate;

  tr.appendChild( title );

  var a = document.createElement( "TD" );
  a.setAttribute( "class", "eventAction" );
  a.innerHTML = "<a href=\"javascript:deleteEvent( '" + id + "' )\" class=\"eventLink\">Delete</a>";

  tr.appendChild( a );

  tab.appendChild( tr );

  note.appendChild( tab );

  var d = document.createElement( "DIV" );
  d.setAttribute( "class", "eventData" );
  d.setAttribute( "id", "eventText" );
  d.innerHTML = data;

  note.appendChild( d );

  return note;
}

function deleteEvent ( id )
{
  var evt = document.getElementById( id );
  var eventDate = evt.getAttribute( "eventDate" );
  var evtId = evt.getAttribute( "eventId" );

  var surl = "event?action=deleteEvent&eventDate=" + eventDate +
                 "&eventId=" + evtId + "&returnEvents=false";

  var callback = 
  {
    success : handleDeleteEvent,
    failure : handleFailure,
    argument : evt
  };

  YAHOO.util.Connect.asyncRequest( 'GET', surl, callback );
}

function handleDeleteEvent ( obj )
{
  var evts = document.getElementById( "events" );  
  evts.removeChild( obj.argument );
}

